package com.mdt.doforms.bienen;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.google.android.gms.maps.model.LatLng;
import com.mdt.doforms.bienen.model.DrugStore;
import com.mdt.doforms.bienen.model.DrugStoreLocation;
import com.mdt.doforms.bienen.util.HtdLog;

public class NotdiensteActivity extends BienenActivity {

	private ArrayList<DrugStore> arrDrugStore = new ArrayList<DrugStore>();
	ListView lstDrugStore = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notdienste);
		
		lstDrugStore = (ListView)findViewById(R.id.listDrugStore);
		lstDrugStore.setAdapter(mDrugAdapter);
		lstDrugStore.setOnItemClickListener(mOnItemSelected);
		
		DrugStore o1 = new DrugStore();
		o1.setName("Apotheke XYZ");
		o1.setAddress("Musterstraße 55 \n88888 München");
		o1.setDistance(2.5f);
		o1.setLatitute(10.8544613);
		o1.setLongitude(106.6257449);
		arrDrugStore.add(o1);
		
		o1 = new DrugStore();
		o1.setName("Apotheke XYss");
		o1.setAddress("Musterstraße 54\n 8811 München");
		o1.setDistance(3.7f);
		o1.setLatitute(10.8544613);
		o1.setLongitude(106.6257449);
		arrDrugStore.add(o1);
		
		o1 = new DrugStore();
		o1.setName("Apotheke XYZsas");
		o1.setAddress("Musterstraße 12\n 4444 München");
		o1.setDistance(9.7f);
		o1.setLatitute(10.8544613);
		o1.setLongitude(106.6257449);
		arrDrugStore.add(o1);
		mDrugAdapter.notifyDataSetChanged();
		
	}
	
	private OnItemClickListener mOnItemSelected = new OnItemClickListener(){

		

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
			if (index >= 0){
				
			}
			
		}
		
	}; 
	private BaseAdapter mDrugAdapter = new BaseAdapter() {

		@Override
		public int getCount() {
			return arrDrugStore.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.notdienste_cell_item, null);
			DrugStore drugStore = arrDrugStore.get(position);
			//ImageView imgView = (ImageView)retval.findViewById(R.id.imgView);
			
			TextView title = (TextView) retval.findViewById(R.id.textDrugStoreName);
			title.setText(drugStore.getName());
			TextView address = (TextView) retval.findViewById(R.id.textAddress);
			address.setText(drugStore.getAddress());
			
			String str = String.format("%.2f km", drugStore.getDistance() );
			TextView distance = (TextView) retval.findViewById(R.id.textDistance);
			distance.setText(str);
			
			final int nSelectedIndex = position;
			ImageView imgView = (ImageView)retval.findViewById(R.id.imgView);
			imgView.setOnClickListener( new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					onOpenLocation(v, nSelectedIndex);
					
				}
			});
			
			return retval;
		}
	};
	
	@Override
	protected void setTitle(String title) {
	}

	private void onOpenLocation ( View v, int position ){
		DrugStore drugStore = arrDrugStore.get(position);
		HtdLog.d("onOpenLocation, position = "+position +", name="+drugStore.getName());
		LatLng location = new LatLng( drugStore.getLatitute(), drugStore.getLongitude());
		
		DrugStoreLocation type = new DrugStoreLocation(drugStore.getName(),drugStore.getAddress(), location);
		Intent intent = new Intent(this, MapActivity.class);
		intent.putExtra(BienenConstants.SHOW_MAP_TYPE_EXTRA_STRING, BienenConstants.SHOW_LOCATION);
		intent.putExtra(BienenConstants.SHOW_MAP_DATA_EXTRA_STRING, type);
		startActivity(intent);
	}
}
